package com.zh.eventdispatcher.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;

import com.zh.eventdispatch.activity.ActivityEventFrameLayout;
import com.zh.eventdispatcher.R;

/**
 * @author wally
 * @date 2021/06/19
 * 接收Activity事件的悬浮提示View
 */
public class FloatView extends ActivityEventFrameLayout {
    /**
     * View高度
     */
    private int mViewHeight;
    /**
     * 是否显示
     */
    private boolean isShow = true;
    /**
     * 显示动画
     */
    private ValueAnimator mShowAnimator;
    /**
     * 隐藏动画
     */
    private ValueAnimator mHideAnimator;

    public FloatView(Context context) {
        this(context, null);
    }

    public FloatView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_float, this);
        View close = findViewById(R.id.close);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewHeight = h;
    }

    /**
     * 显示悬浮View
     */
    public void show() {
        if (mShowAnimator != null && mShowAnimator.isRunning()) {
            return;
        }
        mShowAnimator = ValueAnimator.ofFloat(-mViewHeight, 0);
        mShowAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float cValue = (Float) animation.getAnimatedValue();
                setTranslationY(cValue);
            }
        });
        mShowAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isShow = true;
            }
        });
        mShowAnimator.setInterpolator(new AccelerateInterpolator());
        mShowAnimator.setDuration(300);
        mShowAnimator.start();
    }

    /**
     * 隐藏悬浮View
     */
    public void hide() {
        if (mHideAnimator != null && mHideAnimator.isRunning()) {
            return;
        }
        mHideAnimator = ValueAnimator.ofFloat(0, -mViewHeight);
        mHideAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float cValue = (Float) animation.getAnimatedValue();
                setTranslationY(cValue);
            }
        });
        mHideAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isShow = false;
            }
        });
        mHideAnimator.setInterpolator(new AccelerateInterpolator());
        mHideAnimator.setDuration(200);
        mHideAnimator.start();
    }

    /**
     * 显示则隐藏，隐藏则显示
     */
    public void toggle() {
        if (isShow) {
            hide();
        } else {
            show();
        }
    }

    @Override
    public boolean onSupportBackPressed() {
        if (super.onSupportBackPressed()) {
            return true;
        }
        //如果正在显示，则拦截返回键事件，并隐藏悬浮View
        if (isShow) {
            hide();
            return true;
        }
        //隐藏了，不拦截事件
        return false;
    }
}